<?php

namespace App\Services;

use App\Models\Config;
use App\Models\IntegralLog;
use App\Models\IntegralSet;
use App\Models\Store;
use App\Models\User;
use Illuminate\Support\Facades\DB;

/**
 * 积分设置服务支持
 * Class IntegralSetService
 * @package App\Services
 */
class IntegralSetService extends BaseService
{
    /**
     * @var IntegralSet 积分设置模型
     */
    public $model;

    /**
     * @var $storeModel
     * 店铺模型
     */
    public $storeModel;

    /**
     * IntegralSetService constructor.
     */
    public function __construct()
    {
        $this->model = new IntegralSet();

    }


    /**
     * @var string[] 积分发放场景
     */
    public static $scenes_type = [
        1 => '用户下单', 2 => '用户收货'
    ];

    /**
     * @return mixed
     */
    public static function getSetList()
    {
        $list = IntegralSet::where(function ($query) {
            //状态
            if (isset(request()->type) && request()->type != 0) {
                $query->where('type', request()->type);
            }
            if (isset(request()->state) && request()->state != 0) {
                $query->where('state', request()->state);
            }
        })->orderBy('created_at', 'desc')->paginate(request()->per_page ?? 30)->toArray();

        foreach ($list['data'] as $key => $val) {
            if ($val['type'] == 1) {
                $list['data'][$key]['belong_text'] = DB::table('shop_stores_cate')->where('id', $val['belong_id'])->value('cate_name');
                $list['data'][$key]['type_text'] = '商家分类';
            } else {
                $list['data'][$key]['belong_text'] = Store::where('id', $val['belong_id'])->value('store_name');
                $list['data'][$key]['type_text'] = '商家';
            }
            $scenes_type_text = '';
            foreach (explode(',', $val['scenes_type']) as $keys => $v) {
                $scenes_type_text .= self::$scenes_type[$v];
            }
            $list['data'][$key]['scenes_type_text'] = $scenes_type_text;

            if ($val['state'] == 1) {
                $list['data'][$key]['state_text'] = '开启';
            } else {
                $list['data'][$key]['state_text'] = '关闭';
            }
        }
        return $list;
    }


    /**
     * 2021/3/15--15:10
     * @param array $where [查询条件 可不传递]
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getOne
     * @effect: 获取积分设置系详情
     */
    public function getOne($store_id, $scenes_type, $store_cate_id)
    {
        try {
            $ratio = [];
            // 查询表来证实当前店铺分类 或者当前店铺是否开启 了积分抵扣  如果没有开启则走平台  如果平台也没有开启则 返回 0
            $where = ['ratio_status' => 1, 'scenes_type' => $scenes_type, 'type' => 2, 'belong_id' => $store_id];
            $store_ratio = $this->model->where($where)->first();
            //如果设置了比例 则进行赋值
            if (!empty($store_ratio) && !empty($store_ratio['ratio'])) {
                $store_ratio = json_decode($store_ratio['ratio'], true);
                $ratio['integral_upper_limit'] = $store_ratio['integral_upper_limit'];
                $ratio['ratio'] = $store_ratio['ratio_price'];
            }
            // 如果店铺分类不存在 则走店铺分类
            if (empty($ratio) || empty($store_ratio)) {
                $where = ['ratio_status' => 1, 'scenes_type' => $scenes_type, 'type' => 1, 'belong_id' => $store_cate_id];
                $store_cate_ratio = $this->model->where($where)->first();

                if (!empty($store_cate_ratio) && !empty($store_cate_ratio['ratio'])) {
                    $store_ratio = json_decode($store_cate_ratio['ratio'], true);
                    // 重新赋值新的
                    $ratio['integral_upper_limit'] = $store_ratio['integral_upper_limit'];
                    $ratio['ratio'] = $store_ratio['ratio_price'];
                }
                // 如果店铺分类不存在则走 平台设置
                if (empty($store_cate_ratio) || empty($store_ratio)) {
                    $config_model = new Config;
                    $list = $config_model->get();
                    $data = [];
                    foreach ($list as $v) {
                        $data[$v['name']] = $v['val'];
                    }
                    if (!empty($data) && !empty($data['integral'])) {
                        $integral = json_decode($data['integral'], true);
                        $store_ratio = $integral['ratio'];
                        if (!empty($store_ratio)) {
                            // 重新赋值新的
                            $ratio['integral_upper_limit'] = $store_ratio['integral_upper_limit'];
                            $ratio['ratio'] = $store_ratio['ratio_price'];
                        }
                    }
                }
            }
            return $ratio;
        } catch (\Exception $e) {
            return $this->error('失败' . $e->getMessage());
        }
    }


    /**
     * 2021/3/15--21:43
     * @param $store_id
     * @param $store_cate_id
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:get_integral
     * @effect:积分获取
     */
    public function get_integral($store_id, $store_cate_id, $scenes_type)
    {
        // 积分
        $integral = 0;
        $id = 0;
        try {
            // 查询表来证实当前店铺分类 或者当前店铺是否开启 了积分抵扣  如果没有开启则走平台  如果平台也没有开启则 返回 0
            $where = ['state' => 1, 'scenes_type' => $scenes_type, 'type' => 2, 'belong_id' => $store_id];
            $store_ratio = $this->model->where($where)->first();
            //积分
            $integral = $store_ratio['integral'];
            $id = $store_ratio['id'];
            // 如果店铺设置为空 则获取分类的积分设置
            if (empty($store_ratio)) {
                $where = ['state' => 1, 'scenes_type' => $scenes_type, 'type' => 1, 'belong_id' => $store_cate_id];
                $store_ratio = $this->model->where($where)->first();
                $integral = $store_ratio['integral'];
                $id = $store_ratio['id'];
            }
            return ['integral' => $integral, 'id' => $id];
        } catch (\Exception $e) {
            $this->format([], '获取失败');
        }
    }
}
